﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNManifestDOMEventHandlerParseWorker
	{

		public static void parseHandlerObject(Manifest manifest)
		{
			XmlElement root = manifest.Doc.DocumentElement;
			string xsfPrefix = manifest.XsfPrefix;

			ArrayList handlerObjectList = new ArrayList();

			StringBuilder filterBuffer = new StringBuilder();
			filterBuffer.Append("//");
			filterBuffer.Append(NamespaceUtil.toFullName(xsfPrefix, "domEventHandler"));
			filterBuffer.Append("/@handlerObject");

			XmlNodeList handlerObjectAttrList = DOMUtil.selectNodes(root, filterBuffer.ToString(), 0, manifest.NsResolver);
			foreach (XmlAttribute handlerObjectAttr in handlerObjectAttrList)
			{
				HandlerObject handlerObject = new HandlerObject();
				handlerObject.Object = handlerObjectAttr.Value;
				XmlElement domEventHandlerElement = (XmlElement)handlerObjectAttr.OwnerElement;
				handlerObject.Match = domEventHandlerElement.GetAttribute( "match" );

				if( handlerObject.Object.Length > 0 && handlerObject.Match.Length > 0 )
				{
					handlerObjectList.Add( handlerObject );
				}
			}

			manifest.HandlerObjectList = handlerObjectList;
		}
		
		public static void parseDOMEventHandler(Manifest manifest)
		{
			XmlElement root = manifest.Doc.DocumentElement;
			string xsfPrefix = manifest.XsfPrefix;
			
			ArrayList domEventHandlerList = new ArrayList();
			
			StringBuilder filterBuffer = new StringBuilder();
			filterBuffer.Append("//");
			filterBuffer.Append(NamespaceUtil.toFullName(xsfPrefix, "domEventHandler"));
			filterBuffer.Append("/");
			filterBuffer.Append(NamespaceUtil.toFullName(xsfPrefix, "ruleSetAction"));
			
			XmlNodeList ruleSetActionXmlNodeList = DOMUtil.selectNodes(root, filterBuffer.ToString(), 0, manifest.NsResolver);
			foreach (XmlNode ruleSetActionNode in ruleSetActionXmlNodeList)
			{
				DOMEventHandler handler = new DOMEventHandler();
				XmlElement ruleSetActionElement = (XmlElement) ruleSetActionNode;
				string ruleSetName = ruleSetActionElement.GetAttribute("ruleSet");
				if (ruleSetName.Length <= 0)
				{
					throw new InfoJetException("No ruleSetAction - ruleSet attribute.", "XMDHPW01");
				}
				XmlElement handlerElement = (XmlElement) ruleSetActionElement.ParentNode;
				string match = handlerElement.GetAttribute("match");
				if (match.Length <= 0)
				{
					throw new InfoJetException("No domEventHandler - match attribute.", "XMDHPW02");
				}
				handler.Match = match;
				
				RuleSet ruleSet = XSNManifestRuleSetParseWorker.parseRuleSetByName( manifest, ruleSetName );
				//CID 0167 //2008.12.15 //Begin
                //CID 0735 //2010.12.19 //Begin
				ruleSet = InfoJetSoft.Service.EscapeRule.RuleSetFilter.Filter( manifest, null, match, ruleSet );
                //CID 0735 //2010.12.19 //End
				//CID 0167 //2008.12.15 //End
				if( ruleSet != null ){
					handler.RuleSet = ruleSet;
					domEventHandlerList.Add( handler );
				}
			}
			
			manifest.DomEventHandlerList = domEventHandlerList;

			XSNManifestDOMEventHandlerParseWorker.checkContainsQueryAction( manifest );
		}

		private static void checkContainsQueryAction( Manifest manifest )
		{
			if( manifest.DomEventHandlerList != null )
			{
				foreach( DOMEventHandler domEventHandler in manifest.DomEventHandlerList )
				{
					if( domEventHandler.RuleSet != null )
					{
						foreach( Rule rule in domEventHandler.RuleSet.RuleList )
						{
							if( rule.RuleActionList != null )
							{
								foreach( RuleAction ruleAction in rule.RuleActionList )
								{
									if( ruleAction.RuleActionType == RuleAction.iQueryAction )
									{
										domEventHandler.ContainsQueryAction = true;
										break;
									}
								}
							}
						}
					}
				}
			}
		}

	}
}